package com.collectionutils;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
import com.google.common.collect.Tables;
import org.junit.Test;


public class TablesDemo {


    /**
     *  Tables
     */
    @Test
    public void testTable(){
        Table<String, String, Integer> hashBasedTable = HashBasedTable.create();
        hashBasedTable.put("二班","数学",97);
        hashBasedTable.put("一班","英语",85);
        hashBasedTable.put("三班","语文",70);
        //Table<C, R, V>转置成Table<R, C, V>,如果你在用Table构建加权有向图，这个方法就可以把有向图反转
        Table<String, String, Integer> transpose = Tables.transpose(hashBasedTable);
        //获取所有行的set
        transpose.cellSet().forEach(x->
                System.out.println("rowKey:"+x.getRowKey()+
                        ", columnKey:"+x.getColumnKey()+", value:"+x.getValue()));
        /**
         * rowKey:数学, columnKey:二班, value:97
         * rowKey:英语, columnKey:一班, value:85
         * rowKey:语文, columnKey:三班, value:70
         */
        //自定义table,允许你指定Table用什么样的map实现行和列
        // 使用HashMap作为key,默认是LinkedHashMap
        Table<String, String, Integer> table = Tables.newCustomTable(
                Maps.newHashMap(), Maps::newHashMap);
        table.put("三班","语文",70);
        table.put("二班","数学",97);
        table.put("一班","英语",85);

        // 自定义实现hashmap,不按添加数据排序
        table.cellSet().forEach(x->
                System.out.println("rowKey:"+x.getRowKey()+
                        ", columnKey:"+x.getColumnKey()+", value:"+x.getValue()));
        /**
         * rowKey:一班, columnKey:英语, value:85
         * rowKey:二班, columnKey:数学, value:97
         * rowKey:三班, columnKey:语文, value:70
         */
    }
}
